package com.diyizu.controller.impl;

import com.diyizu.controller.IRegController;
import com.diyizu.domain.User;
import com.diyizu.email.EmailUtil;
import com.diyizu.factory.Factory;
import com.diyizu.service.ILoginService;
import com.diyizu.service.IRegService;
import com.diyizu.service.impl.ILoginServiceImpl;
import com.diyizu.service.impl.IRegServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class IRegControllerImpl implements IRegController {
    private static Scanner sc = new Scanner(System.in);
    private static Random r = new Random();
    private static IRegService iRegService = Factory.getIRegService();
    private static ILoginService iLoginService = Factory.getILoginService();
    private static final Logger LOGGER = LoggerFactory.getLogger(IRegControllerImpl.class);

    @Override
    //注册
    public void userReg() {
        String userName;
        String passWord;
        String email;
        //注册用户名
        while (true) {
            System.out.println("请输入用户名");
            userName = sc.next();
            //判断用户名格式是否正确
            boolean b1 = iRegService.isUserFormat(userName);


            if (b1) {
                //判断用户名是否存在
                boolean b = iLoginService.judgeUser(userName);
                if (b) {
                    break;
                } else {
                    System.out.println("你输入的用户名已经存在");
                }

            } else {
                System.out.println("请输入正确格式的用户名");
            }
        }

        //注册密码
        while (true) {
            System.out.println("请输入密码");
            System.out.println("提示：密码最少8位数，最多16位数且不能有空格");
            passWord = sc.next();
            boolean b2 = iRegService.isPassFormat(passWord);
            if (b2) {
                break;
            } else {
                System.out.println("请输入正确格式的密码");
            }
        }

        //获取验证码
        lo :while (true) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 6; i++) {
                sb.append(r.nextInt(10));
            }
            String verCode = sb.toString();
            System.out.println("请输入邮箱");
            email = sc.next();
            boolean b2 = iRegService.isEmail(email);
            if (b2) {
                System.out.println("你输入的邮箱已绑定");
            } else {
                EmailUtil.sendMail(email, verCode, "注册验证码");
                boolean b3 = iRegService.isIdentifyingCode(verCode);
                if (b3) {
                    break;
                } else {
                    while (true) {
                        System.out.println("请输入正确的验证码");
                        boolean b4 = iRegService.isIdentifyingCode(verCode);
                        if (b4) {
                            break lo;
                        }
                    }
                }


            }
        }
        try {
            iRegService.userReg(userName, passWord, email);
        } catch (Exception e) {
            e.printStackTrace();
        }
        LOGGER.info(userName + "注册成功");
        System.out.println("注册成功");
    }

    @Override
    public void updatePass() {
        //改密码
        while (true) {
            System.out.println("请输入你的用户名");
            String username = sc.next();
            boolean b1 = false;
            boolean b2 = false;
            try {
                b1 = iLoginService.judgeUser(username);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (b1) {
                System.out.println("你输入的用户名不存在，请输入正确的用户名");
            } else {
                System.out.println("请输入旧密码");
                String oldPassWord = sc.next();
                try {
                    b2 = iLoginService.judgePass(username, oldPassWord);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (b2) {
                    System.out.println("请输入新密码");
                    String passWord = sc.next();
                    boolean b3 = iRegService.isPassFormat(passWord);
                    if (b3) {
                        iRegService.updatePass(username, passWord);
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void updateEmail() {
        String email;
        String newEmail;
        //改邮箱
        while (true) {
            System.out.println("请输入你的用户名");
            String userName = sc.next();
            boolean b1 = false;
            try {
                b1 = iLoginService.judgeUser(userName);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (b1) {
                System.out.println("用户名不存在,请重新输入");
            } else {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < 6; i++) {
                    sb.append(r.nextInt(10));
                }
                String verCode = sb.toString();
                User user = iRegService.getUser(userName);
                 email = user.getEmail();
                EmailUtil.sendMail(email, verCode, "更改邮箱验证码");
                boolean b3 = iRegService.isIdentifyingCode(verCode);
                if (b3) {
                    System.out.println("请输入新邮箱");
                     newEmail = sc.next();
                    boolean b2 = iRegService.isEmail(newEmail);
                    if (b2) {
                        System.out.println("你输入的邮箱已绑定");
                    } else {
                        iRegService.updateEmail(userName, newEmail);
                        return;
                    }
                } else {
                    while (true) {
                        System.out.println("请输入正确的验证码");
                        boolean b4 = iRegService.isIdentifyingCode(verCode);
                        if (b4) {
                            System.out.println("请输入新邮箱");
                            newEmail = sc.next();
                            boolean b2 = iRegService.isEmail(newEmail);
                            if (b2) {
                                System.out.println("你输入的邮箱已绑定");
                            } else {
                                iRegService.updateEmail(userName, newEmail);
                                return;
                            }
                        }
                    }
                }

            }
        }
    }
}


